package com.weds.edpf.core.web;

import com.alibaba.fastjson.JSONObject;
import com.weds.core.annotation.Logs;
import com.weds.core.base.BaseCommPager;
import com.weds.core.base.BaseController;
import com.weds.core.base.BasePageSearch;
import com.weds.core.resp.JsonResult;
import com.weds.edpf.core.entity.DeviceEntity;
import com.weds.edpf.core.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author zcz
 * @Description 设备管理
 * @Date 2020-06-12
 */
@Validated
@RestController("deviceControllerNew")
@RequestMapping(value="/deviceNew")
@Api(value = "Device管理", description = "Device管理")
public class DeviceController extends BaseController{
	@Resource(name = "deviceServiceNew")
	private DeviceService deviceService;

    
	private Logger log = LogManager.getLogger();

	/**
	 * 设备定义列表
	 * @param record
	 * @return
	 */
	@Logs
	@ApiOperation(value = "设备定义列表", notes = "设备定义列表")
	@RequestMapping(value = "/selectDeviceDefineList", method = RequestMethod.POST)
	public JsonResult<BaseCommPager<DeviceEntity>> selectDeviceDefineList(@RequestBody BasePageSearch<DeviceEntity> record) {
		setPageHelper(record);
		DeviceEntity entity = record.getSearch();
		if (entity == null) {
			entity = new DeviceEntity();
		}
		List<DeviceEntity> list = deviceService.selectDeviceDefineList(entity);
		return succMsgData(new BaseCommPager<>(list));
	}
    @Logs
	@ApiOperation(value = "新增Device信息", notes = "新增Device信息")
	@RequestMapping(value = "/insert", method = RequestMethod.PUT)
	public JsonResult<Object> insert(@RequestBody @Valid DeviceEntity record) {
        				deviceService.insertSelective(record);
		return succMsg();
	}

	@Logs
	@ApiOperation(value = "更新Device信息", notes = "更新Device信息")
	@RequestMapping(value = "/updateByPrimaryKey", method = RequestMethod.POST)
	public JsonResult<Object> updateByPrimaryKey(@RequestBody @Valid DeviceEntity record) {
        		deviceService.updateByPrimaryKeySelective(record);
		return succMsg();
	}

		@Logs
	@ApiOperation(value = "查询Device清单", notes = "查询Device清单")
	@RequestMapping(value = "/selectListPageByEntity", method = RequestMethod.POST)
	public JsonResult<BaseCommPager<DeviceEntity>> selectListPageByEntity(@RequestBody BasePageSearch<DeviceEntity> record) {
		setPageHelper(record);
		DeviceEntity entity = record.getSearch();
        if (entity == null) {
        	entity = new DeviceEntity();
        }
                List<DeviceEntity> list = deviceService.selectListByEntity(entity);
        		return succMsgData(new BaseCommPager<DeviceEntity>(list));
	}

    @Logs
    @ApiOperation(value = "查询Device列表", notes = "查询Device列表")
    @RequestMapping(value = "/selectListByEntity", method = RequestMethod.POST)
    public JsonResult<List<DeviceEntity>> selectListByEntity(@RequestBody DeviceEntity record) {
                List<DeviceEntity> list = deviceService.selectListByEntity(record);
                return succMsgData(list);
    }
	
		@Logs
	@ApiOperation(value = "删除Device信息", notes = "删除Device信息")
	@RequestMapping(value = "/deleteByPrimaryKey", method = RequestMethod.DELETE)
	public JsonResult<Object> deleteByPrimaryKey(@RequestParam String bh) {
        deviceService.deleteByPrimaryKey(bh);
        return succMsg();
	}

	/**
	 * 设备定义编辑-查询基本内容
	 * @param devSerial
	 * @return
	 */
	@Logs
	@ApiOperation(value = "设备定义编辑-查询基本内容", notes = "设备定义编辑-查询基本内容")
	@RequestMapping(value = "/selectByDevSerial", method = RequestMethod.GET)
	public JsonResult<DeviceEntity> selectByPrimaryKey(@RequestParam String devSerial) {
		DeviceEntity entity = deviceService.selectByPrimaryKey(devSerial);
		if(Objects.nonNull(entity)){
			if(null == entity.getRechargeInterval()){
				entity.setRechargeInterval(Long.parseLong("0"));
			}
			if(null == entity.getWarnTemp()){
				entity.setWarnTemp("37.0");
			}
		}
        return succMsgData(entity);
	}

	@Logs
	@ApiOperation(value = "批量新增Device信息", notes = "批量新增Device信息")
	@RequestMapping(value = "/insertBatch", method = RequestMethod.PUT)
	public JsonResult<Object> insertBatch(@RequestParam String data) {
		List<DeviceEntity> list	= JSONObject.parseArray(data, DeviceEntity.class);
		deviceService.insertBatch(list);
		return succMsg();
	}

	@Logs
	@ApiOperation(value = "设备定义编辑-根据设备编号查询场所信息", notes = "设备定义编辑-根据设备编号查询场所信息")
	@RequestMapping(value = "/selectDepByDevSerial", method = RequestMethod.GET)
	public JsonResult<Object> selectDepByDevSerial(@RequestParam String devSerial) {
		Map<String, String> map = deviceService.selectDepByDevSerial(devSerial);
		return succMsgData(map);
	}

	@Logs
	@ApiOperation(value = "设备定义编辑-根据设备编号查询模块信息", notes = "设备定义编辑-根据设备编号查询模块信息")
	@RequestMapping(value = "/selectModuleByDevSerial", method = RequestMethod.GET)
	public JsonResult<Object> selectModuleByDevSerial(@RequestParam String devSerial) {
		Map<String, String> map = deviceService.selectModuleByDevSerial(devSerial);
		return succMsgData(map);
	}

	@Logs
	@ApiOperation(value = "设备定义修改-保存", notes = "设备定义修改-保存")
	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	public JsonResult<Object> update(@Valid @RequestBody DeviceEntity record) {
		return deviceService.updateByPrimaryKeySelective(record);
	}

	@Logs
	@ApiOperation(value = "设备定义删除", notes = "设备定义删除")
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public JsonResult<Object> deleteBatch(@Param("devSerials")String devSerials) {
		return deviceService.deleteBatch(devSerials);
	}

}